package com.zrrd.yunchmall;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zrrd.yunchmall.entity.Product;
import com.zrrd.yunchmall.entity.TProduct;
import com.zrrd.yunchmall.repository.TProductRepository;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.ParsedMax;
import org.elasticsearch.search.aggregations.metrics.ParsedMin;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class EsTest2 {
    //自动装配一个持久化接口实例
    @Autowired
    TProductRepository productRepository;

    //1 保存一个商品信息（文档）
    @Test
    public void t1() {
        TProduct product = new TProduct(9, "黑莓手机", 2999.0, 100);
        product = productRepository.save(product);
        System.out.println(product);
        System.out.println("OK");
    }

    //2 根据id进行查找 GET t_product/_search
    @Test
    public void t2() {
        TProduct product = productRepository.findById(1).get();
        System.out.println(product);
    }

    //5 删除全部文档
    @Test
    public void t5() {
        productRepository.deleteAll();
    }

    //3 保存多个记录
    @Test
    public void t3() {
        List<TProduct> list = new ArrayList<>();
        for (int i = 11; i <= 100; i++) {
            list.add(new TProduct(i, "手机型号-" + i, 3999.0, 100));
        }
        productRepository.saveAll(list);
        System.out.println("OK.");
    }

    //4 查询全部记录
    @Test
    public void t4() {
        Iterable<TProduct> list = productRepository.findAll();
        list.forEach(item -> {
            System.out.println(item);
        });
    }

    //6 分页查询
    @Test
    public void t6() throws IOException {
        // 页码从0开始
        int pageNum = 1, pageSize = 10;
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by("pid"));

        Page<TProduct> page = productRepository.findAll(pageRequest);
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(page));
    }

    //7 排序查找
    @Test
    public void t7() throws IOException {
//        Iterable list = productRepository.findAll(Sort.by("pid", "pprice"));
        Iterable list = productRepository.findAll(Sort.by(Sort.Direction.DESC, "pprice"));
        list.forEach(item -> System.out.println(item));
    }
    //8 测试自定义的查询方法
    @Test
    public void t8() throws IOException {
        List<TProduct> list = productRepository.findByPname("诺基亚");
        list.forEach(item -> {
            System.out.println(item);
        });
    }
    @Test
    public void t9() {
        List<TProduct> list = productRepository.findByPprice(1000);
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t10() {
        List<TProduct> list = productRepository.findByPnameAndPprice("摩托罗拉", 1000);
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t11() {
        List<TProduct> list = productRepository.findByPnameOrPprice("诺基亚", 1000);
        list.forEach(item -> System.out.println(item));
    }

    @Test
    public void t12() {
        List<TProduct> list = productRepository.findByPpriceLessThan(1001);//<1001
        list.forEach(item -> System.out.println(item));
    }

    @Test
    public void t13() {
        List<TProduct> list = productRepository.findByPpriceGreaterThan(2999);//>2999
        list.forEach(item -> System.out.println(item));

    }

    @Test
    public void t14() {
        List<TProduct> list = productRepository.findByPpriceBetween(1000, 3000);// 1000<=pprice and pprice<=3000
        list.forEach(item -> System.out.println(item));
    }
    //9 测试模糊查询
    @Test
    public void t15() {
        List<TProduct> list = productRepository.findByPnameLike("手机");
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t16() {
        List<TProduct> list = productRepository.findByPnameStartingWith("小米");
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t17() {
        List<TProduct> list = productRepository.findByPnameEndingWith("5");
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t18() {
        List<TProduct> list = productRepository.findByPnameContaining("OPPO");
        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t19() {
        List<TProduct> list = productRepository.findByPnameContains("华为");
        list.forEach(item -> System.out.println(item));
    }
    //10 带有特殊参数的方法测试
    @Test
    public void t20() throws JsonProcessingException {
        PageRequest pageRequest = PageRequest.of(0, 5);
//        List<TProduct> list = productRepository.findByPpriceBetween(999, 5999);
        Page<TProduct> page = productRepository.findByPpriceBetween(999, 5999, pageRequest);
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(page));
//        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t21() throws JsonProcessingException {
//        PageRequest pageRequest = PageRequest.of(0, 5);
//        List<TProduct> list = productRepository.findByPpriceBetween(999, 5999);
        Page<TProduct> page = productRepository.findByPpriceBetween(999, 5999, Pageable.unpaged());
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(page));
//        list.forEach(item -> System.out.println(item));
    }
    @Test
    public void t22() throws JsonProcessingException {
//        Sort sort = Sort.unsorted();//不使用排序规则
        Sort sort = Sort.by(Sort.Direction.DESC, "pprice").and(Sort.by("stock").descending());
        Sort sort2 = Sort.by("pprice").descending().and(Sort.by("stock").ascending());
        List<TProduct> list = productRepository.findByPnameLike("手机", sort);
//        list.forEach(item -> System.out.println(item));
        list.forEach(System.out::println);
    }



}
